var connPool = require("./ConnPool")
var async = require('async')

module.exports = {
	ask:function(req,res){
		var loginbean = req.session.loginbean;
		pool = connPool();
		console.log('写入数据库，提交成功')
		pool.getConnection(function(err,conn){
      var userAddSql = 'insert into question (typeid,typename,title,content,uid,nicheng,createtime) value(?,?,?,?,?,?,current_timestamp)';
      var param = [req.body['typeid'], req.body['typename'], req.body['title'], req.body['content'], loginbean.id, loginbean.nicheng];
			conn.query(userAddSql,param,function(err,rs){
				if(err){
					res.send('数据库错误，原因' + err.message  + err)
					return;
				}
				res.send('<script>alert("提交成功");location.href="../";</script>')
				// res.redirect('../')
			})
			
			conn.release();
		})
	},
	queList:function(req,res,loginbean){
		// var loginbean = req.session.loginbean;
		var pool = connPool();
		pool.getConnection(function(err,conn){
			
			if(err){
				connsole.log('youcuoma')
				res.send("获取连接发生错误,错误原因：" + err.message );
				return;
			}
			// 从第几页开始
			var page = 1;
			// 从url获取page，表示这是第几页
			if(req.query['page'] !== undefined){
				page = parseInt(req.query['page'])
				if(page < 1){
					page = 1
				}
			}
			// 每页显示几条信息
			var pageSize = 4;
			// 从第几页开始
			var pointStart = (page -1) * pageSize;
			// 查询数据库  查出列表    按掉pid倒序排列
			var countSql = 'select count(*) as count from question';  //把count(*) 名字替换为 count
			var listSql = 'select qid,title,looknum,renum,finished,updtime,createtime from question order by qid desc limit ?,?';
			var param = [pointStart,pageSize];
			var count = 0;
			var countPage = 0;
			async.series({
				one:function(callback){
					// 查询总共有多少条记录
					conn.query(countSql,[],function(err,rs){
					// conn.query(countSql,[],function(err,rs){
						count = rs[0]['count'] //取到总条数
						countPage = Math.ceil(count/pageSize) //向上取整，计算出总页数
						if(page > countPage){
							page = countPage
							pointStart = (page -1) * pageSize;
							param = [pointStart,pageSize];
						}
						callback(null,rs)
					})
				},
				two:function(callback){
					// 查询数据库开始位置  和 几条，返回想要的数据
					conn.query(listSql,param,function(err,rs){
						console.log(rs)
						callback(null,rs)
						
					})
				}
			},function(err,results){
				if(err){
					console.log('数据库错误是' + err)
				}	
				var rs = results['two']
				res.render('index',{'loginbean':loginbean,'page':page,'rs':rs,'count':count,'countPage':countPage,})
			})

			conn.release();
		})
	},
	queDetail: function (req,res) {
    var qid = req.query['qid']
    var sqlupdate = 'update question set looknum=looknum+1 where qid=?';
    var spldetail = 'select qid,title,content,uid,looknum,renum,finished,updtime,createtime from question where qid=?';
    var param = [qid];
    if(qid != undefined){
      var pool = connPool();
      pool.getConnection(function(err,conn){
        if(err){
          res.send('获取数据库连接发生错误' + err.message)
          return;
        }
        async.series({
          one:function(callback){
            conn.query(sqlupdate,param,function(err,rs){
              callback(null,rs)
            })
          },
          two:function(callback){
            conn.query(spldetail,param,function(err,rs){
              callback(null,rs)
            })
          }
        },function(err,results){
          console.log(results)
          var rs = results['two']
          res.render('queDetail',{rs:rs})
        })



      })
    }else{
      res.send("没传入qid")
    }
    
	}








}
